package cn.com.cifi.mars.web.action.report;


import cn.com.cifi.mars.bean.vo.report.InvestmentReturnReportVo;
import cn.com.cifi.mars.common.base.BaseAction;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.VersionTypeEunm;
import cn.com.cifi.mars.service.DmInvestmentReturnService;
import cn.com.cifi.mars.service.IdmUserService;
import cn.com.cifi.mars.util.InvestmentReturnTemplate;
import cn.com.cifi.mars.util.LocalDateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 投资回款分析表 前端控制器
 * </p>
 *
 * @author JingYuanXiang
 * @since 2020-12-23
 */
@Slf4j
@RestController
public class InvestmentReturnController extends BaseAction implements IInvestmentReturnController {

    @Autowired
    private DmInvestmentReturnService dmInvestmentReturnService;
    @Autowired
    private IdmUserService idmUserService;

    @Override
    public R refreshDmInvestmentReturn(String apiVersion, String projectId, String yearMonth) {
        dmInvestmentReturnService.refreshDmInvestmentReturn(projectId,yearMonth);
        return ok();
    }

    @Override
    public R<List<InvestmentReturnReportVo>> dmInvestmentReturn(String apiVersion, String yearMonth, String versionType, String vsVersionType) {
        //查询用户权限项目id列表
        List<String> projectIdList = idmUserService.queryUserProjectPermissions(getOperator().getUsername());
        if(CollectionUtils.isEmpty(projectIdList)){
            throw new MsgException(CodeEnum.NOT_PROJECT_PERMISSIONS_ERROR);
        }

        List<InvestmentReturnReportVo> voList = dmInvestmentReturnService.dmInvestmentReturn(projectIdList,yearMonth,versionType,vsVersionType);
        return ok(voList);
    }

    @Override
    public void exportInvestmentReturn(String apiVersion, HttpServletResponse response, String yearMonth, String versionType, String vsVersionType) {
        //查询用户权限项目id列表
        List<String> projectIdList = idmUserService.queryUserProjectPermissions(getOperator().getUsername());
        if(CollectionUtils.isEmpty(projectIdList)){
            throw new MsgException(CodeEnum.NOT_PROJECT_PERMISSIONS_ERROR);
        }

        List<InvestmentReturnReportVo> voList = dmInvestmentReturnService.dmInvestmentReturn(projectIdList,yearMonth,versionType,vsVersionType);

        InvestmentReturnTemplate investmentReturnTemplate;
        String name;
        if(versionType.equals(VersionTypeEunm.INVESTMENT.getKey())){
            name = "投资回款承诺分析表-" + yearMonth.replace("-","")+"-"+ LocalDateTimeUtils.formatTime(LocalDateTime.now(), "yyyyMMddHH");
            investmentReturnTemplate = new InvestmentReturnTemplate(name,"投资版","动态版",Integer.valueOf(yearMonth.split("-")[0]));
        }else {
            name = "投资回款能力分析表-" + yearMonth.replace("-","")+"-"+ LocalDateTimeUtils.formatTime(LocalDateTime.now(), "yyyyMMddHH");
            investmentReturnTemplate = new InvestmentReturnTemplate(name,"能力版","动态版", Integer.valueOf(yearMonth.split("-")[0]));
        }
        investmentReturnTemplate.appendRows(voList,0);

        try (OutputStream outputStream = response.getOutputStream()){
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(name.concat(".xlsx"), "utf-8"));
            investmentReturnTemplate.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.error("###### errorMsg: ", e);
        }
    }

    @Override
    public R refreshAbilityReturnPlan(String apiVersion, String projectId, String yearMonth) {
        dmInvestmentReturnService.refreshAbilityReturnPlan(projectId,yearMonth);
        return ok();
    }
}
